package com.goldsign.pay.common.util.rsa;



import com.goldsign.pay.common.util.Base64Utils;

import java.io.*;
import java.util.Map;

/**
 * 
 * @author xiaoxianwen
 *
 */
public class RsaTester {  
  
    static String publicKey;  
    static String privateKey;  
  
    static {  
        try {  
            Map<String, Object> keyMap = RsaUtils.genKeyPair();  
            //RSAUtils.getPublicKey(keyMap);  
            publicKey = getPrivateKey("PublicKey_ser.keystore");
            //RSAUtils.getPrivateKey(keyMap);  
            privateKey = getPrivateKey("PrivateKey_ser.keystore");
            System.err.println("公钥: \n\r" + publicKey);
            System.err.println("私钥： \n\r" + privateKey);
        } catch (Exception e) {  
              
        }  
    }

    public static String stringToEncrypt(String str) throws Exception {
        byte[] data = str.getBytes();
        byte[] encodedData = RsaUtils.encryptByPublicKey(data, publicKey);
        return new String(encodedData);
    }
      
    private static String getPrivateKey(String fileName) throws Exception{
    	InputStream is = new FileInputStream(new File("D:\\system\\Decktop\\temp\\"+fileName));
    	StringBuilder sb = new StringBuilder();
    	BufferedReader br = null;
    	InputStreamReader inputStreamReader=new InputStreamReader(is);
    	br = new BufferedReader(inputStreamReader); 
        String readLine = null;  
        while ((readLine = br.readLine()) != null) {  
            sb.append(readLine);  
        }  
        br.close();  
		return sb.toString();
	}

	public static void main(String[] args) throws Exception {  
        test();  
//        testSign();
    }  
  
    static void test() throws Exception {  
        System.err.println("公钥加密——私钥解密");  
        String source = "7df69a764fc3b471a08b985bc81fafe8";  
        System.err.println("\r原文：\r\n" + source);  
        byte[] data = source.getBytes();  
        byte[] encodedData = RsaUtils.encryptByPublicKey(data, publicKey);
        //Base64Utils.encode(encodedData);
        String temp = "pDEutMnGsk8MdQl0rHs3UxOwQr3pzudYhwU3/yzO802BLpiebAFMOYi4vfhi7I+OcK6Sf3VLfMkWO9H+zQpOtEZaHc8+fmVJGEcYR9+p4C5fxKVwU6aOPMVD+myWicx/aoUq23pMaYnaonf6A2loM1duRfqfbOs2iKdaMw1Rbf4=";
        System.out.println("【Base64Utils.encode后的字串：】"+temp);
        System.err.println("公钥加密后文字：\r\n" + new String(encodedData,"UTF-8"));  
        byte[] decodedData = RsaUtils.decryptByPrivateKey(Base64Utils.decode(temp), privateKey);
        String target = new String(decodedData);  
        System.err.println("私钥解密后文字: \r\n" + target);  
    }  
  
    static void testSign() throws Exception {  
        System.err.println("私钥加密——公钥解密");  
        String source = "这是一行测试RSA数字签名的无意义文字";  
        byte[] data = source.getBytes();  
        byte[] encodedData = RsaUtils.encryptByPrivateKey(data, privateKey);  
        byte[] decodedData = RsaUtils.decryptByPublicKey(encodedData, publicKey);  
        String target = new String(decodedData);  
        System.err.println("私钥签名——公钥验证签名");  
        String sign = RsaUtils.sign(encodedData, privateKey);  
        System.err.println("签名:\r" + sign);  
        boolean status = RsaUtils.verify(encodedData, publicKey, sign);  
        System.err.println("验证结果:\r" + status);  
    }  
      
}  